package rmi.client;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import rmi.intf.Intf;

	public class ClientClass extends JFrame {
		JPanel chat;
                TextArea showMe;
		JTextField input;
		JButton post;
		String s1, s2;
		private static final long serialVersionUID = 1L;

		public ClientClass(String toName, String fromName){
			//setSize(300,300);
			toName = toName.toUpperCase();
			fromName = fromName.toUpperCase();
			s1 = toName;
			s2 = fromName;
			setTitle("i-To " + toName);
                        setIconImage(Toolkit.getDefaultToolkit().getImage("ilogo.gif"));
                        showMe = new TextArea(5, 40);
                        showMe.setEditable(false);
                        add(showMe);
			chat = new JPanel(new GridLayout(1,0));
			chat.setLayout(new FlowLayout(FlowLayout.LEFT));
			input = new JTextField(25);
			chat.add(input);
                        post = new JButton("post");
			post.setEnabled(false);
			chat.add(post);
			add(chat, BorderLayout.SOUTH);
			setVisible(true);
			pack();
			
			input.addKeyListener(new KeyListener() {
				
				public void keyPressed(KeyEvent ke) { }
				
				public void keyReleased(KeyEvent ke) {
					if(!(input.getText()).equals(""))
						post.setEnabled(true);
					else
						post.setEnabled(false);
					int key = ke.getKeyCode();
					if(key == KeyEvent.VK_ENTER){
						if(!input.getText().equals("")){
							sendMessage((" " + s2 + " -> " +input.getText()));
                                                        showMe.append("\ni-ME -> " + input.getText());
							input.setText("");
							post.setEnabled(false);
                                                        input.requestFocus();
						}
					}	
				}
				
				public void keyTyped(KeyEvent ke) {	}
			});
			
			post.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent ae) {
    				String st = input.getText();
    				input.setText("");
    				input.requestFocus();
					post.setEnabled(false);
    				sendMessage(" " + s2 + " -> " + st);
                                showMe.append("\ni-ME -> " + st);
    			}
			});
		}
    				
		
		private void sendMessage(String msg)
		{
			try{
				Properties prop = new Properties();
				prop.put("java.naming.factory.initial","com.sun.jndi.cosnaming.CNCtxFactory");
				prop.put("java.naming.provider.url","corbaloc:iiop:localhost:20900/NameService");
				Context ctx = new InitialContext(prop);
				Intf ob = (Intf)ctx.lookup(s1);
				ob.sendName(msg);
			}catch (Exception e) {
				System.out.println("Error : "+e.getMessage());
			}
			
		}
}